<template>
  <div>
    <div claas="card" style="width: 50%; padding: 40px" >
      <!--      padding-right 右边空一点距离-->
      <el-form :model="data.form" ref="formRef"   label-width="100px" label-position="right" style="padding-right: 40px">
        <el-form-item label="学生头像">
          <!--          :show-file-list="false" 是否显示文件上传的结果-->
          <!--          list-type="picture" 显示图片的效果-->
          <el-upload  list-type="picture-card" class="avatar-uploader" action="http://localhost:9090/files/upload"
                      :on-success="handleImgUploadSuccess">
            <img v-if="data.form.avatar" :src="data.form.avatar" class="avatar">
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="学生账号" prop="username">
          <!--          进行验证-->
          <el-input v-model="data.form.username" autocomplete="off" disabled />
        </el-form-item>
        <el-form-item label="学生密码" prop="password">
          <el-input v-model="data.form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生名称" >
          <el-input v-model="data.form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生手机">
          <el-input v-model="data.form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生邮箱" >
          <el-input v-model="data.form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="data.form.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker style="width: 100%;" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="data.form.birth"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="update">保 存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {reactive} from "vue";
import {ElMessage} from "element-plus";
import router from "@/router";
import request from "@/utils/request";

const data = reactive({
  form: JSON.parse(localStorage.getItem('student-user') || "{}")
//  获取登录的信息
})

// 文件上传之后处理的函数
const handleImgUploadSuccess = (res) => {
  // res 表示回调参数
  console.log(res);
  data.form.avatar = res.data;
}

const update = () => {
  request.put('/student/update', data.form).then(res => {
    if (res.code === '200') {
      // console.log(res)
      ElMessage.success("操作成功")
      router.push('/login')
    } else {
      ElMessage.error(res.msg)
    }
  })
}

</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
